﻿using Microsoft.Phone.Controls;
using System;
using System.IO;
using System.Windows;
using System.Windows.Navigation;

namespace WP8.AlmacenamientoDatos.Pages
{
    public partial class Api : PhoneApplicationPage
    {
        public Api()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RestaurarIsolatedStorageSettings();
            RestaurarIsolatesStorageFile();
            RestaurarApplicationData();
            base.OnNavigatedTo(e);
        }

        #region Events

        private void Guardar_IsolatedStorageSettings_Click(object sender, RoutedEventArgs e)
        {
            System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["NombreValue"] = this.txtNombre.Text;
        }

        private void Guardar_IsolatedStorageFile_Click(object sender, RoutedEventArgs e)
        {
            var isolatedStorageFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            var isolatedStorageFileStream = isolatedStorageFile.CreateFile("applicationData");
            using (var streamWriter = new StreamWriter(isolatedStorageFileStream))
            {
                var nombreValue = this.txtNombre.Text;
                streamWriter.Write(nombreValue);
            }
        }

        /// <summary>
        /// .LocalSettings
        /// .RoamingFolder
        /// .RoamingSettings
        /// .TemporaryFolder
        /// .LocalFolder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Guardar_ApplicationData_Click(object sender, RoutedEventArgs e)
        {
            var storageFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("midata", Windows.Storage.CreationCollisionOption.ReplaceExisting);
            var stream = await storageFile.OpenStreamForWriteAsync();
            using (var streamWriter = new StreamWriter(stream))
            {
                var nombreValue = this.txtNombre.Text;
                streamWriter.Write(nombreValue);
            }
        }

        #endregion Events

        #region Methods

        private void RestaurarIsolatedStorageSettings()
        {
            if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains("NombreValue"))
                this.txtIsolatedStorageSettings.Text = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["NombreValue"].ToString();
        }

        private void RestaurarIsolatesStorageFile()
        {
            var isolatedStorageFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            if (!isolatedStorageFile.FileExists("applicationData"))
                return;

            var isolatedStorageFileStream = isolatedStorageFile.OpenFile("applicationData", FileMode.Open);
            using (var streamReader = new StreamReader(isolatedStorageFileStream))
            {
                this.txtIsolatedStorageFile.Text = streamReader.ReadToEnd();
            }
        }

        private async void RestaurarApplicationData()
        {
            var fullPath = string.Format("{0}\\{1}", Windows.Storage.ApplicationData.Current.LocalFolder.Path, "midata");
            var fileExists = System.IO.File.Exists(fullPath);
            if (fileExists)
            {
                var stream = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("midata");
                using (var streamReader = new StreamReader(stream))
                {
                    this.txtApplicationData.Text = streamReader.ReadToEnd();
                }
            }
        }

        #endregion Methods
    }
}